package cn.zwx.security.example.common.exception;

import cn.zwx.security.example.common.ResponseEntry;
import cn.zwx.security.example.common.enums.ResultCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description:
 * @projectName:spring-boot-security-example
 * @see:cn.zwx.security.example.common.exception
 * @author:zhangwenxue
 * @createTime:2021/6/23 15:02
 * @version:1.0
 */
public class GlobalExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    /**
     * 处理自定义异常
     *
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ResponseEntry bizExceptionHandler(BizException e) {
        return ResponseEntry.defineError(e);
    }

    /**
     * 处理其他异常
     *
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseEntry exceptionHandler( Exception e) {
        logger.error("e.getMessage()",e);
        return ResponseEntry.otherError(ResultCodeEnum.INTERNAL_SERVER_ERROR.getErrorCode(),e.getMessage());
    }

}
